package org.one.afternoon.controller;

import org.one.afternoon.common.Result;
import org.one.afternoon.domain.User;
import org.one.afternoon.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;


@RestController
@RequestMapping(value = "/user")
public class UserController {

    @Autowired
    private UserService userService;


    @CrossOrigin // 跨域访问
    @GetMapping("/users")
    public Result<List<User>> findAllUser(){
        List<User> userList=userService.findAll();
        Result<List<User>> result=Result.success(userList);
        return result;
    }


    //复合查询
    @CrossOrigin
    @PostMapping("/query")
    @ResponseBody
    public Result<List<User>> queryUser(@RequestBody User user) {
        List<User> userList=userService.findUser(user);
        Result<List<User>> result=Result.success(userList);
        return result;
    }


    @CrossOrigin
    @PostMapping("/register")
    @ResponseBody
    public Result<User> register(@RequestBody User user) {
        User addUser = userService.register(user);
        Result<User> result;
        if (addUser == null){
            result = Result.error("创建用户失败");
        }else {
            result = Result.success(addUser);
        }
        return result;
    }


    @CrossOrigin
    @PostMapping("/insert")
    @ResponseBody
    public Result<User> insert(@RequestBody User user) {
        User addUser = userService.addUser(user);
        Result<User> result;
        if (addUser == null){
            result = Result.error("添加用户失败");
        }else {
            result = Result.success(addUser);
        }
        return result;
    }

    @CrossOrigin
    @PostMapping("/delete")
    @ResponseBody
    public Result<Integer> delete(@RequestParam Integer index) {
        userService.delete(index);
        Result<Integer> result=Result.success(index);
        return result;
    }



    @CrossOrigin
    @PostMapping("/update")
    @ResponseBody
    public Result<User> update(@RequestBody User user) {
        User updateUser = userService.update(user);
        Result<User> result=Result.success(updateUser);
        return result;
    }



    @CrossOrigin
    @PostMapping("/login")
    @ResponseBody
    public Result<User> login(@RequestBody User user) {
        User loginUser = userService.login(user);
        Result<User> result=Result.success(loginUser);
        return result;
    }

    @CrossOrigin
    @GetMapping("/info")
    @ResponseBody
    public Result<User> info() {
        // todo: 先看是否登录，是的话 返回token和信息，否的话返回null
        HashMap<String, Object> responseInfo = new HashMap<>();
        HashMap<String, Object> responseData = new HashMap<>();
        responseData.put("roles","admin");
        responseData.put("name","Xm");
        responseData.put("avatar","");
        responseInfo.put("code",20000);
        responseInfo.put("msg","登录成功");
        responseInfo.put("data",responseData);
        return null;
    }



    @CrossOrigin
    @PostMapping("/logout")
    @ResponseBody
    public Result<User> logout(@RequestBody User user) {
        User loginUser = userService.logout(user);
        Result<User> result=Result.success(loginUser);
        return result;
    }

    @CrossOrigin
    @GetMapping ("/online")
    public Result<Integer> queryOnline() {
        Integer online=userService.queryOnline();
        Result<Integer> result=Result.success(online);
        return result;
    }

    @CrossOrigin
    @GetMapping ("/total")
    public Result<Integer> queryTotal() {
        Integer total=userService.queryTotal();
        Result<Integer> result=Result.success(total);
        return result;
    }

    @CrossOrigin
    @GetMapping ("/tdy")
    public Result<Integer> queryTdy(@RequestParam int time) {
        Integer tdy=userService.queryTdy(time);
        Result<Integer> result=Result.success(tdy);
        return result;
    }

    @CrossOrigin
    @GetMapping ("/new")
    public Result<Integer> queryNew(@RequestParam int time) {
        Integer New=userService.queryNew(time);
        Result<Integer> result=Result.success(New);
        return result;
    }
}
